//
//  RegisterViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/6.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class RegisterViewController: BaseViewController {
    
    
    @IBOutlet var logoTopHeight: NSLayoutConstraint!
    
    @IBOutlet var itemHeight: NSLayoutConstraint!
    
    @IBOutlet var chooseProtocolBtn: UIButton!
    
    @IBOutlet var phoneTF: UITextField!
    
    @IBOutlet var codeTF: UITextField!
    
    @IBOutlet var getCodeBtn: JKCountDownButton!
    
    
    @IBOutlet var passwordTF: UITextField!
    
    
    @IBOutlet var recomeTF: UITextField!
    
    
    @IBOutlet var registerBtn: UIButton!
    
    @IBOutlet var backLoginBtn: UIButton!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        self.navigationController?.navigationBar.isHidden = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.registerBtn.roundCorner(radius: 5)
        self.backLoginBtn.roundCorner(radius: 5)
    }
    
    
    @IBAction func getCodeBtnPress(_ sender: JKCountDownButton) {
        if phoneTF.text?.characters.count != 11 {
            MBProgressHUD.alertShow(in: self.view, message: "请输入正确的手机号", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            
            return
        }
        getCodeBtn.start(withSecond: 60)
        getCodeBtn.didFinished { (_, _) -> String? in
            self.getCodeBtn.isEnabled = true
            return "获取验证码"
        }
        getCodeBtn.isEnabled = false
        
        requestGetVerifyCode(phone: phoneTF.text!, type: "0")
    }
    
    
    @IBAction func registerBtnPress(_ sender: UIButton) {
        if phoneTF.text! == "" || passwordTF.text! == "" || codeTF.text! == ""{
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再注册", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            
            return
        }
        if self.chooseProtocolBtn.isSelected == false{
            MBProgressHUD.alertShow(in: self.view, message: "注册前，请查阅协议后勾选", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requestRegister(phone: phoneTF.text!, password: passwordTF.text!, recommonCode: recomeTF.text!, code: codeTF.text!)
    }
    
    @IBAction func backLoginBtnPress(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
    //选择协议按钮
    @IBAction func chooseProtocolBtnPress(_ sender: UIButton) {
        chooseProtocolBtn.isSelected = !chooseProtocolBtn.isSelected
        
    }
    
    //查看协议按钮
    @IBAction func lookProtocolBtnPress(_ sender: UIButton) {
        let protocolVC = BaseWebViewController()
        protocolVC.title = "袋鼠好车商户入驻协议";
       protocolVC.loadUrl = merchantProtocolUrl
        self.navigationController?.pushViewController(protocolVC, animated: true)
    }
    
    
    //网络请求
    //注册
    func requestRegister(phone:String,password:String,recommonCode:String,code:String) -> Void {
        
        let param =
            [
             "phone":phone,
             "password":password,
             "code":code,
             "recommend_code":recommonCode,
            ]
        Alamofire.request(registerUrl, method:.post, parameters:param)
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    var basemodel = BaseResponseOfModel<CommonModel>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "注册成功"
                        hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })
                    case false:
                    MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    //获取验证码
    func requestGetVerifyCode(phone:String,type:String) -> Void {
        Alamofire.request(getVerificationCodeUrl, method:.post, parameters:["phone":phone,"type":type])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    var basemodel = BaseResponseOfModel<CommonModel>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        MBProgressHUD.alertShow(in: self.view, message:"发送验证码成功", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    case false:
                         MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
